﻿using System;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
namespace HostEdit
{
    /// <summary>
    /// Interaction logic for WriteIp.xaml
    /// </summary>
    public partial class WriteIp : Window
    {
        public WriteIp()
        {
            InitializeComponent();
        }


        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {

            ExecuteTest();

        }

        void ExecuteTest()
        {

            image1.Visibility = System.Windows.Visibility.Collapsed;

            if (radioButton1.IsChecked == true)
            {
                if (!Testv4(textBox1.Text))
                    image1.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                if (!Testv6(textBox1.Text))
                    image1.Visibility = System.Windows.Visibility.Visible;
            }

            button2.IsEnabled = (image1.Visibility != System.Windows.Visibility.Visible);


        }



        bool Testv6(string txt)
        {
            Regex i = new Regex(@"^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$");
            MatchCollection l = i.Matches(txt);
            if (l.Count == 0) return false;

            foreach (Match item in l)
            {
                return true;
            }


            return true;
        }

        bool Testv4(string txt)
        { 
         Regex i = new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}");
            MatchCollection l = i.Matches(txt);
            if (l.Count==0) return false;

            foreach (Match item in l)
            {

                String[] it = item.Value.Split('.');

                foreach (var item1 in it)
                {
                    
                int u = 0;
                if (!int.TryParse(item1, out u))
                    return false;

                if (u > 255)
                    return false;
                }

            }


            return true;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            
            this.Close();
        }

        private void radioButton1_Checked(object sender, RoutedEventArgs e)
        {
            ExecuteTest();
        }

        private void radioButton2_Checked(object sender, RoutedEventArgs e)
        {
            ExecuteTest();
        }

        public String Retour
        {
            get 
            {
                return textBox1.Text; 
            }
            set
            {

                textBox1.Text = value;

                if (Testv4(textBox1.Text) || !Testv6(textBox1.Text))
                    radioButton1.IsChecked = true;
                else
                    radioButton2.IsChecked = true;

            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Retour = String.Empty;
            this.Close();
        }
    }
}
